Skip to main content

create

create

This method creates a new appointment rating, validates the input, prevents duplicates, and optionally auto-approves or rejects based on rating criteria.

  1. Extract Input Data

    • Get the fields from createRatingDto: customer_id, booking_id, overall_rating, provider_rating, staff_rating, clinic_rating, and comments.
    • Determine if the appointment is internal by checking if booking_id starts with 'BKD'.
  2. Check for Existing Rating

    • Query the rating_appointment table to check if a rating already exists for the booking_id.
    • If found, throw a ConflictException indicating the appointment has already been rated.
  3. Validate Customer Exists

    • Check if a customer with customer_id exists.
    • If not, throw a NotFoundException for invalid request data.
  4. Fetch Appointment Data

    • If internal appointment, query the appointments table using booking_id.
    • Else, query the appointments_ehr table using the numeric booking_id on emt_appointment_id field.
    • Select related customer, agent, and businessLocation details.
    • If appointment not found, throw a NotFoundException.
  5. Verify Customer Matches Appointment

    • Confirm the customer.id from appointment matches the input customer_id.
    • If mismatch, throw a BadRequestException.
  6. Create Rating Entity

    • Use repository’s create method to prepare the rating with provided scores, comments, status set to 'pending', and the current date/time.
  7. Save Rating

    • Persist the rating entity to the database.
    • If saving fails, throw a RatingCreationFailedException.
  8. Update Rating Notification Status

    • Update the rating notification record linked to the appointment to status RATED.
    • Log an error if this update fails, but continue execution.
  9. Auto-Approval / Auto-Rejection Logic

    • If no comments provided AND provider_rating >= 4:

      • Fetch the first agent with agent type AgentAdmin to act as approving agent.
      • If none found, log an error.
      • Otherwise, check if the customer's email ends with '@alethian.com' (indicating internal account).
      • If internal and environment is production, auto-reject rating; otherwise, auto-approve.
      • Call approveAppointmentRating with this agent and status.
  10. Return Success Response

    • Return a success message confirming the rating submission.